package com.cvo.scrumtoolkit.client.pannels;

import java.util.List;

import com.cvo.scrumtoolkit.client.controller.Navigator;
import com.cvo.scrumtoolkit.client.entities.BacklogItem;
import com.cvo.scrumtoolkit.client.entities.Project;
import com.cvo.scrumtoolkit.client.model.ProjectStore;
import com.cvo.scrumtoolkit.client.services.ScrumToolService;
import com.cvo.scrumtoolkit.client.services.ScrumToolServiceAsync;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class SprintBacklogPanel extends Composite {
	
	private Project project;
	private static SprintBacklogPanelUiBinder uiBinder = GWT.create(SprintBacklogPanelUiBinder.class);
	private final ScrumToolServiceAsync scrumtoolService = GWT.create(ScrumToolService.class);
	interface SprintBacklogPanelUiBinder extends
			UiBinder<Widget, SprintBacklogPanel> {
	}
	
		
	private List<BacklogItem> items;
	

	public SprintBacklogPanel() {
		
		initWidget(uiBinder.createAndBindUi(this));
		// id van project verkrijgen van navigator 
		ProjectStore projectStore=ProjectStore.getProjectStore();	
		try {
			project = projectStore.getProjectById(Long.parseLong(Navigator.getNavigator().getProjectId()));
			items =project.getBacklogItems();
			view();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		/*this.items = new LinkedList<BacklogItem>(); 
		this.items.add(new BacklogItem().setBacklogItemName("Dummy 1").setGeschatteTijd(25));
		this.items.add(new BacklogItem().setBacklogItemName("Dummy 2").setGeschatteTijd(40));
		this.items.add(new BacklogItem().setBacklogItemName("Dummy 3").setGeschatteTijd(30));
		this.items.add(new BacklogItem().setBacklogItemName("Dummy 4").setGeschatteTijd(25));
		this.items.add(new BacklogItem().setBacklogItemName("Dummy 5").setGeschatteTijd(10));
		this.items.add(new BacklogItem().setBacklogItemName("Dummy 6").setGeschatteTijd(50));*/
	}
	
	public SprintBacklogPanel(List<BacklogItem> items) {
		initWidget(uiBinder.createAndBindUi(this));
		this.items = items;
		view();
	}
	
	@UiField
	VerticalPanel verticalPanel;
	
	@UiField
	Button button;
	
	
	public void view(){
		
		
		
		
		CellTable<BacklogItem> cellTable = new CellTable<BacklogItem>();
		verticalPanel.add(cellTable);
		
		// Create itemname column.
	    TextColumn<BacklogItem> nameColumn = new TextColumn<BacklogItem>() {
	      @Override
	      public String getValue(BacklogItem item) {
	        return item.getBacklogItemName();
	      }
	    };
	  
	   
	    
	    // Create itemprioriteit column.
	    
	    TextColumn<BacklogItem> prioriteitColumn = new TextColumn<BacklogItem>() {
	      @Override
	      public String getValue(BacklogItem item) {
	        return (items.indexOf(item)+"");
	      }
	    };
	       
	 // Create geschatte tijd column.      
	    Column<BacklogItem, Number> geschatteTijd = new Column<BacklogItem, Number>(new NumberCell()) {
	    	@Override
	    	public Number getValue(BacklogItem item) {
	    		return (item.getGeschatteTijd());
	    	}
	    };
	   	    
	    Column<BacklogItem, Boolean> column = new Column<BacklogItem, Boolean>(new CheckboxCell()) {
	    	@Override
	    	public Boolean getValue(BacklogItem item) {
	    		return (Boolean) null;
	    	}
	    };
	    
	    // Add the columns.
	    cellTable.addColumn(nameColumn, "Itemname");
	    cellTable.addColumn(prioriteitColumn, "Prioriteit");
	    cellTable.addColumn(geschatteTijd, "Geschatte Tijd");
	    cellTable.addColumn(column);
	    cellTable.setColumnWidth(column, "30px");
	    prioriteitColumn.setSortable(true);
	    
	      
	   	    
	    cellTable.setRowCount(this.items.size(), true);
	    cellTable.setRowData(0, this.items);
	    button.setText("Selectie Toevoegen");
	   
	 
	}
	
	@UiHandler("button")
	public void onButtonClick(ClickEvent e){
		
	}
	
	

}
	
	
	
	

